package com.redare.dev.smart.generator.service.provider;

import com.redare.dev.smart.generator.sdk.pojo.CodeTemplate;
import com.redare.dev.smart.generator.sdk.pojo.OnlFormHead;
import com.redare.dev.smart.generator.sdk.utils.BuilderUtil;
import com.redare.dev.smart.generator.sdk.utils.VelocityUtil;
import com.redare.dev.smart.generator.service.provider.filetype.JavaFileType;
import com.redare.dev.smart.generator.service.provider.filetype.LanguageFileType;
import org.apache.commons.lang.StringUtils;
import org.apache.velocity.VelocityContext;

import java.io.File;
import java.util.Map;

/**
 * 默认文件提供者
 *
 */
public class DefaultProviderImpl extends AbstractFileProvider {


    protected LanguageFileType languageFileType;

    public DefaultProviderImpl(String outputPath, LanguageFileType languageFileType) {
        super(outputPath);
        this.languageFileType = languageFileType;
    }

    @Override
    public void  create(CodeTemplate template, OnlFormHead context, Map<String, Object> extraMap,String basePackage){

        VelocityContext velocityContext = new VelocityContext(BuilderUtil.transBean2Map(context));
        velocityContext.put("serialVersionUID", BuilderUtil.computeDefaultSUID(context.getModel(), context.getFields()));
        // $!dateFormatUtils.format($!now,'yyyy-MM-dd')
        velocityContext.put("dateFormatUtils", new org.apache.commons.lang.time.DateFormatUtils());
        if (StringUtils.isNotBlank(template.getSubPath())){
            velocityContext.put("templateSubPath",template.getSubPath().replace("/","."));
        }
        if (!StringUtils.isEmpty(basePackage)) {
            extraMap.put("basePackage",basePackage);
        }
        if (extraMap != null && extraMap.size() > 0) {
            for (Map.Entry<String, Object> entry: extraMap.entrySet()) {
                velocityContext.put(entry.getKey(), entry.getValue());
            }
        }

        try {
            String fileName = VelocityUtil.evaluate(velocityContext, template.getFilename());
            String temp = VelocityUtil.evaluate(velocityContext, template.getTemplate());
            String subPath = VelocityUtil.evaluate(velocityContext, template.getSubPath());

            File outputPathFile = new File(outputPath);
            if (!outputPathFile.exists()){
                outputPathFile.mkdirs();
            }
            File subOutPathFile = new File(outputPathFile,subPath);
            if (!subOutPathFile.exists()){
                subOutPathFile.mkdirs();
            }
            if (JavaFileType.INSTANCE== this.languageFileType) {
                if (!StringUtils.isEmpty(basePackage)) {
                    extraMap.put(fileName, new StringBuilder(basePackage).append(".").append(subPath).append(".").append(fileName));
                }
            }
//            String formattedSource = new Formatter().formatSource(temp);
            createFile(subOutPathFile, fileName + "." + this.languageFileType.getDefaultExtension(), temp, this.languageFileType,basePackage);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}